<?php
//error_reporting(0);

/**
 * 提供一个mid,抓取到库里
 */

include_once($_SERVER["DOCUMENT_ROOT"]. "/core/inc.php");
include_once($_SERVER["DOCUMENT_ROOT"] . "/core/admin.inc.php");
include_once($_SERVER["DOCUMENT_ROOT"]. "/include/mblogv2/mod_get_mblogv2_from_api.php");
include_once($_SERVER["DOCUMENT_ROOT"]. "/include/foodv2/inc.php");

$g_cgival = array();
$g_pro = array();
$g_show = array();

function show_pro()
{
	global $g_show;
	echo output::json("json",$g_show);
}


function check_cgi_pro()
{
	global $g_cgival, $g_pro, $g_show;
	cgi::both($g_cgival["mid"], "mid", "");
	//$g_cgival["mid"] = string::un_html($g_cgival["mid"]);
	
	cgi::both($g_cgival["tag_name"], "tag_name", "");
	$g_cgival['tag_name'] = string::un_html(string::un_script_code(mod_status_core::sbc_to_dbc($g_cgival['tag_name'])));
	
	$tag_list = array();
	$tag = explode(" ",trim($g_cgival["tag_name"]));
	for ($i=0; $i<count($tag); $i++)
	{
		if (!empty($tag[$i]))
		{
			$tag_list[] = $tag[$i];
			if (!preg_match("/^([\x{4e00}-\x{9fa5}0-9A-Za-z_\-]+)$/u",$tag[$i]))
			{
					throw new Exception('1002:微博标签仅支持中英文、数字、"_"或减号', 0);
			}
			if (string_utf8::string_len($tag[$i])>7)
			{
				throw new Exception("标签内容超过7个字");
				break;
			}
		}
	}
	//删除重复
	$tag_list = array_unique($tag_list);
	if (count($tag_list)>5)
	{
		throw new Exception("最多添加5个标签");
	}
	$g_cgival["tag_name"] = $tag_list;
	
	cgi::both($g_cgival["is_res"], "is_res", 0);
	$g_cgival["is_res"] = intval($g_cgival["is_res"]);
	
	cgi::both($g_cgival["res_id"], "res_id", 0);
	$g_cgival["res_id"] = intval($g_cgival["res_id"]);
	
	cgi::both($g_cgival["site_id"], "site_id", 0);
	$g_cgival["site_id"] = intval($g_cgival["site_id"]);
}


function get_data()
{
	global $g_cgival, $g_pro, $g_show,$mid_have_get;
	
	
	
	$site_id = $g_cgival["site_id"];
	$mid = $g_cgival["mid"];
	$is_res = $g_cgival["is_res"];
	$res_id = $g_cgival["res_id"];
	$tag_names = $g_cgival["tag_name"];
	
	if(!$site_id)
	{
		$site_id = admin::admin_get_user_site_id();
	}
	
	if(!$g_cgival["mid"])
	{
		throw new Exception("mid不能为空！");
	}
	// 规范是否探店与餐厅id的对应关系 add by zhanghuan at 2012/06/21
	if ($is_res && !$res_id)
	{
		$is_res = 0;
	}
	elseif (!$is_res && $res_id)
	{
		$res_id = 0;
	}
	
	//构造sql,一次性插入
	$sql_status = " insert ignore into food_status (status_id,mid,create_uid,status,update_time,last_update_time,update_ip,site_id,res_id,is_pic,is_rstrt) values ";
	$sql_status_array = array();
	
	$sql_status_tag = "insert ignore into food_status_tag ( status_id,tag,last_update_time,create_uid,create_nickname,create_time,status,create_ip,site_id,is_pic,is_rstrt) values ";
	$sql_status_tag_array = array();
	
	
	$db_food_status_tag = new db_food_status_tag();
	$db_food_status = new db_food_status();
	$mblog_from_api = new mblog_from_api();
	$base_search = new base_search();
	$mod_food_status_content = new mod_food_status_content();
	
	$mids_info = $mblog_from_api->api_get_mblog_by_mids($mid);
	$add_status_ids = array();
	
	if($mids_info["data"]["count"]  < 1)
	{
		throw new Exception("微博接口没有抓到数据！");
	}
	$have_status_id = array();
	foreach($mids_info["data"]["result"] as $value1)
	{
		if(!$value1)
		{
			continue;
		}	
		$create_time = date("Y-m-d H:i:s",strtotime($value1["created_at"]));
		//获取微博坐标信息
		$coordinates = "";
		if($value1["geo"]["coordinates"])
		{
			$coordinates = $value1["geo"]["coordinates"][1].",".$value1["geo"]["coordinates"][0];
		}
		//获取坐标对应地址
		if($coordinates)
		{
			 $address_info = $mblog_from_api->api_geo_to_address($coordinates);
			if($address_info["data"]["result"])
			{
				$location = $address_info["data"]["result"][0]["province_name"]." ".$address_info["data"]["result"][0]["city_name"]." ".$address_info["data"]["result"][0]["address"];
			} 
		}
		
		//$split_word = $base_search->get_mblog_word_cut($value1["text"]);
		
		
		if(!$value1["original_pic"])
		{
			throw new Exception("本条微博没有图片不允许发布！");
		}
		$last_status_id= $mod_food_status_content->insert_data_and_size($value1["user"]["id"],$value1["user"]["screen_name"],$value1["text"],$value1["original_pic"],0,$value1["source"],$create_time,$value1["mid"],$coordinates,$location,$split_word);
		$last_status_id = $last_status_id["status_id"];
		
		
		$add_status_ids[] = $last_status_id; 
		
		if($value1["original_pic"])
		{
			$is_pic = 1;
		}
		else
		{
			$is_pic = 0 ;
		}
		
		//food_status开始
		if($mid_have_get)
		{
			//更新food_tag,如果已经有了，
			$res = $db_food_status->cron_mod_update_food_status_by_status_id($last_status_id,$is_res,$site_id);
		}
		
		if(!$res)
		{
			$sql_status_array[] = "({$last_status_id}, {$value1["mid"]} , {$value1["user"]["id"]},  1 , '{$create_time}','{$create_time}',0,$site_id,$res_id,$is_pic,$is_res)";
		
		}
		//food_status结束
		
		//food_status_tag开始
		
		if($mid_have_get)
		{
			//更新 food_status_tag
			$res1 =$db_food_status_tag->cron_mod_update_food_status_by_status_id($last_status_id,$is_pic,$site_id);
		}
		if(!$res1)
		{
			foreach ($tag_names as $k=>$tag_name) {
				$now_time = strtotime(Date::get_date_time()) - $k;
				$sql_status_tag_array[] = "({$last_status_id}, '{$tag_name}', {$now_time}, {$value1["user"]["id"]} , '{$value1["user"]["screen_name"]}','{$create_time}',1,0 ,$site_id,$is_pic,$is_res)";
			}
		}
		else
		{
			$have_status_id[] = $last_status_id;
		}
	}
	
	if($sql_status_tag_array)
	{
		$sql  = $sql_status_tag. join(",",$sql_status_tag_array);
		$db_food_status_tag->query($sql);
	}
	
	
	if($sql_status_array)
	{
		$sql  = $sql_status. join(",",$sql_status_array);
		$db_food_status_tag->query($sql);
	}

	
	//前台URL拼装
	// 拿到当前站点的site_id
	$site_id = admin::admin_get_user_site_id();
	$user_global_config =  user_global_config::get_instance();
	$domain = $user_global_config->get_domain_by_site_id($site_id);			// 当前用户的站点ID
	
	$status_ids = join(" ",$add_status_ids);
	$edit_link = "生成的ID ： {$status_ids} <br><br>";
	$have_link ="以下为库中已经有，需要查看审核情况：<br>";
	
	foreach($add_status_ids as $value)
	{
		
		if(in_array($value,$have_status_id))
		{
			$have_link .=" <a target='_blank' href='/admin/foodv2/share/share_manage.php?status_id={$value}'>查看{$value}是否审核</a>&nbsp;&nbsp;&nbsp;";
			$have_link .= "<a target='_blank' href='/admin/foodv2/mblog/edit_food_status.php?status_id={$value}'>编辑ID{$value}</a>&nbsp;&nbsp;&nbsp;";
			$have_link .=  "<a target='_blank' href='http://{$domain}/explore/detail-{$value}.html'>{$value}前台链接&nbsp;&nbsp;</a><br><br>";
			
		}
		else
		{
			$edit_link .= "<a target='_blank' href='/admin/foodv2/mblog/edit_food_status.php?status_id={$value}'>编辑ID{$value}</a>&nbsp;&nbsp;&nbsp;";
			$edit_link .=  "<a target='_blank' href='http://{$domain}/explore/detail-{$value}.html'>{$value}前台链接&nbsp;&nbsp;</a><br><br>";
			
		}
	}
	
	
	$edit_link .= $have_link;
	$g_show["edit_link"] = $edit_link;

	
	
}



try
{
	admin_check_login();
// 	admin_check_user_priv("foodv2/main_nav.php");
	
	check_cgi_pro();
	get_data();
}
catch(Exception $e)
{
	$g_show["error"] = 1;
	$g_show["errmsg"] = $e->getMessage();
}

show_pro();
exit;

